package com.wanhe.base.util;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by yangchaoxun
 */

public class BitmapUtil {

    private static final long MB = 1024 * 1024;

    /**
     * 保存图片到指定路径的方法
     *
     * @param path   图片保存的相对路径
     * @param name   图片的名字
     * @param bitmap 要保存的图片
     * @throws IOException 读写图片文件出现的异常信息
     */
    public static String saveImg(String path, String name, Bitmap bitmap)
            throws IOException {
        File file = new File(path, name + ".png");
        // 若图片文件在SD卡的文件夹不存在
        if (!file.getParentFile().exists()) {
            // 创建该文件夹
            file.getParentFile().mkdirs();
        }
        // 若文件不存在，则创建
        if (!file.exists()) {
            file.createNewFile();
        }
        // 创建文件输出流
        FileOutputStream out = new FileOutputStream(file);
        // 保存图片至SD卡指定文件夹
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        return file.getAbsolutePath();
    }


    /**
     * 获得指定路径的图片
     * @param path 图片的本地路径
     * @return 图片对象
     * @throws IOException
     */
    public static Bitmap getBitmap(String path) {
        Bitmap imageBitmap = null;
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            File file = new File(path);
            if (file.exists() && (file.length() / MB) > 1) {
                options.inSampleSize = 2;
            }
            imageBitmap = BitmapFactory.decodeFile(path, options);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return imageBitmap;
    }

    // 2)Drawable → Bitmap
    public static Bitmap convertDrawable2BitmapSimple(Drawable drawable) {
        BitmapDrawable bd = (BitmapDrawable) drawable;
        return bd.getBitmap();
    }

    // Bitmap → Drawable
    public static Drawable convertBitmap2Drawable(Bitmap bitmap) {
        BitmapDrawable bd = new BitmapDrawable(bitmap);
        // 因为BtimapDrawable是Drawable的子类，最终直接使用bd对象即可。
        return bd;
    }


    public static void getImageFromAlbum(Activity activity, int REQUEST_CODE_PICK_IMAGE) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");//相片类型
        activity.startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
    }


    public static void getImageFromCamera(Activity activity, int REQUEST_CODE_CAPTURE_CAMEIA) {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
            activity.startActivityForResult(getImageByCamera, REQUEST_CODE_CAPTURE_CAMEIA);
        } else {
            Toast.makeText(activity, "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
        }
    }

}
